#!/bin/bash

# 检查参数数量
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <source_image> <target_image>"
    exit 1
fi

SOURCE_IMAGE=$1
TARGET_IMAGE=$2

# 拉取镜像
echo "Pulling image: $SOURCE_IMAGE"
docker pull $SOURCE_IMAGE

# 获取镜像ID
IMAGE_ID=$(docker images --format "{{.ID}}" --filter "reference=$SOURCE_IMAGE")

# 打标签
echo "Tagging image: $IMAGE_ID to $TARGET_IMAGE"
docker tag $IMAGE_ID $TARGET_IMAGE

# 推送镜像
echo "Pushing image: $TARGET_IMAGE"
docker push $TARGET_IMAGE

# 删除镜像
echo "Deleting images: $SOURCE_IMAGE and $TARGET_IMAGE"
docker rmi $SOURCE_IMAGE
docker rmi $TARGET_IMAGE

echo "Process completed successfully."